package com.yc.vcode.web;

import com.yc.bean.Result;
import com.yc.vcode.bean.VcodeProperties;
import com.yc.vcode.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("vcode")
public class VcodeDo {


    @Autowired
    VcodeProperties properties;

    @GetMapping("config")
    VcodeProperties properties(){
        return properties;
    }

    @RequestMapping(path = "image", produces = MediaType.IMAGE_PNG_VALUE)
    public void  image(HttpServletResponse response,
                       HttpSession session) throws Exception {
        String vcode = VerifyCodeUtils.outputImage(response);
        session.setAttribute("vcode", vcode);

        long time = System.currentTimeMillis();
        time += properties.getTimeout() * 1000;
        session.setAttribute("vcode-timeout", time);
    }

    //TODO 定义发送验证码邮件的方法，提供给未来的控制器方法调用即可
    public Result email(String email,String msgName) {
        return null;
    }
}
